﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ETL.Business.Models;
using ETL.Common.Logging;
using ETL.DataAccess;
using ETL.Entity;

namespace ETL.Business.Handlers.ProjectTimeHandler
{
    public class AddNewProjectTime
    {
        private readonly ProjectTimeRepository _projectTimeRepository;

        public AddNewProjectTime(ProjectTimeRepository projectTimeRepository)
        {
            _projectTimeRepository = projectTimeRepository;
        }
        public AddNewProjectTime()
        {
            _projectTimeRepository = new ProjectTimeRepository();
        }
        public virtual void Execute(string projectId, string categoryId, string taskId, string userName, string userId)
        {
            try
            {
                // convert
                short cPrjId = short.Parse(projectId);
                short cCatId = short.Parse(categoryId);
                short cTaskId = short.Parse(taskId);

                // set latest from DB  DateTime dt, float hours
                _projectTimeRepository.CreateNewProjectTimes(cPrjId, cCatId, cTaskId, userName, userId);
                
            }
            catch (Exception ex)
            {
                LogService.Service.Debug(ex);
            }
        }

        public virtual void Execute(string projectId, string categoryId, string taskId, string userName, string userId, string dt, string hours)
        {
            try
            {
                // convert
                short cPrjId = short.Parse(projectId);
                short cCatId = short.Parse(categoryId);
                short cTaskId = short.Parse(taskId);
                //int cUserId = int.Parse(userId);

                // set latest from DB  DateTime dt, float hours
                //_projectTimeRepository.CreateNewProjectTimes(cPrjId, cCatId, cTaskId, userName, cUserId, DateTime.Parse(dt), float.Parse(hours));
                _projectTimeRepository.CreateNewProjectTimes(cPrjId, cCatId, cTaskId, userName, userId, DateTime.Parse(dt), float.Parse(hours));

            }
            catch (Exception ex)
            {
                LogService.Service.Debug(ex);
            }
        }

        //Only user for update DB when user click on "Other Activity" ???
        public virtual void Execute(string projectId, string categoryId, string userName, string userId, string timeOther, string timeReason)
        {
            try
            {
                // convert

                //short? cPrjId = null;
                //short _cPrjId;
                //if (short.TryParse(projectId, out _cPrjId))
                //{
                //    cPrjId = _cPrjId;
                //}
                
                //If this is from user clicking on "Other Activity" then only set projectId to 0 (i.e. zero)
                short? cPrjId = 0;
                short _cPrjId;
                if (short.TryParse(projectId, out _cPrjId))
                {
                    cPrjId = _cPrjId;
                }

                short? cCatId = null;
                short _cCatId;
                if (short.TryParse(categoryId, out _cCatId))
                {
                    cCatId = _cCatId;
                }

                // set latest to DB
                _projectTimeRepository.CreateNewProjectTimes(cPrjId, cCatId, userName, userId, timeOther, timeReason);

            }
            catch (Exception ex)
            {
                LogService.Service.Debug(ex);
            }
        }

    }
}
